﻿Public Class MThd

    Public Property Format As Short
    Public Property TrackCount As UShort
    Public Property TimeDivision As Short
    Public Property IsFPS As Boolean

    Private Sub New()

    End Sub

    Public Shared Function FromStream(InputReader As BigEndianBinaryReader) As MThd

        If InputReader.Read4CharString <> "MThd" Then Throw New MidiFileFormatException(InputReader.Position - 4, "Not a midi file.")
        If InputReader.ReadInt32 <> 6 Then Throw New MidiFileFormatException(InputReader.Position - 4, "Wrong header size.")

        Dim Result As New MThd

        With Result

            .Format = InputReader.ReadInt16
            .TrackCount = InputReader.ReadUInt16

            Dim TempDivision As Short = InputReader.ReadInt16
            .TimeDivision = TempDivision And CShort(&H7FFF)
            .IsFPS = (TempDivision And &H8000) <> 0

        End With

        Return Result

    End Function
End Class
